import 'package:flutter/material.dart';
import 'package:online_study/base/app_title.dart';
import 'package:online_study/base/course_search_item.dart';
import 'package:online_study/theme/colors.dart';
import 'package:online_study/theme/size.dart';
import 'package:online_study/theme/string.dart';

//课程搜索
class SearchCoursePage extends StatefulWidget {
  @override
  _SearchCoursePageState createState() => _SearchCoursePageState();
}

class _SearchCoursePageState extends State<SearchCoursePage> {
  bool hasHistory = true;
  List<String> history = ["财务", "财务进阶之道", "商务谈判技巧", "十天学习修炼谈判大神", "HTML5高手进阶"];
  List<String> result = ["财务进阶之道", "财务小白入门课程", "财务十大必知技巧，成为公司核心人员"];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading: BackButton(color: ThemeColor.colorBlack),
        backgroundColor: ThemeColor.colorWhite,
        elevation: ThemeSize.appElevation,
        title: AppTitle(ThemeString.search_course_title),
      ),
      body: Column(
        children: [
          Container(
            color: ThemeColor.colorWhite,
            child: Row(
              children: [
                Expanded(child: CourseSearchPage()),
                GestureDetector(
                  child: Container(
                    padding: EdgeInsets.only(
                        left: ThemeSize.width9, right: ThemeSize.width19),
                    child: Text("搜索",
                        style: TextStyle(
                            color: ThemeColor.color1485EE,
                            fontSize: ThemeSize.sp13)),
                  ),
                  onTap: () {
                    doSearch();
                  },
                )
              ],
            ),
          ),
          Expanded(child: buildCenter())
        ],
      ),
    );
  }

  void doSearch() {
    hasHistory = false;
    setState(() {});
  }

  Widget buildCenter() {
    if (hasHistory) {
      return Container(
        padding: EdgeInsets.only(
            left: ThemeSize.width19,
            right: ThemeSize.width19,
            top: ThemeSize.height12),
        color: ThemeColor.colorPagerBg,
        child: Column(
          children: [
            Container(
              alignment: Alignment.topLeft,
              child: Text("搜索记录",
                  style: TextStyle(
                      color: ThemeColor.color333333,
                      fontSize: ThemeSize.sp13,
                      fontWeight: FontWeight.bold)),
            ),
            Wrap(
              children: history
                  .map((e) => Container(
                        padding: EdgeInsets.only(
                            right: ThemeSize.width30, top: ThemeSize.height12),
                        child: Text(e,
                            style: TextStyle(
                                color: ThemeColor.color808080,
                                fontSize: ThemeSize.sp13)),
                      ))
                  .toList(),
            )
          ],
        ),
      );
    } else {
      return Container(
        padding: EdgeInsets.only(
            left: ThemeSize.width19,
            right: ThemeSize.width19,
            top: ThemeSize.height12),
        color: ThemeColor.colorWhite,
        child: Column(
          children: [
            Container(
              alignment: Alignment.topLeft,
              child: Text("搜索结果(3)",
                  style: TextStyle(
                      color: ThemeColor.color333333,
                      fontSize: ThemeSize.sp13,
                      fontWeight: FontWeight.bold)),
            ),
            Expanded(
                child: ListView.builder(
              itemBuilder: (BuildContext context, int index) {
                return Container(
                  padding: EdgeInsets.only(top: ThemeSize.height14),
                  child: Text(result[index],
                      style: TextStyle(
                          color: ThemeColor.color333333,
                          fontSize: ThemeSize.sp13)),
                );
              },
              itemCount: result.length,
              shrinkWrap: true,
            ))
          ],
        ),
      );
    }
  }
}
